package cn.dceast.user.service;

import cn.dceast.user.dao.UserInfoMapper;
import cn.dceast.user.model.UserInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 *
 * Created by zhouxiaa on 2017/7/11.
 */
@Service
public class UserService {

    @Autowired
    private UserInfoMapper userInfoMapper;


    public int register(UserInfo userInfo) {
        return userInfoMapper.insertSelective(userInfo);
    }

    public int verification(String email) {
        return userInfoMapper.verification(email);
    }

    public int login(Map<String, Object> param) {
        return userInfoMapper.login(param);
    }

    public UserInfo getUserInfo(Map<String, Object> param) {
        return userInfoMapper.getUser(param);
    }

    public int updateTime(UserInfo date) {
        return userInfoMapper.updateByPrimaryKeySelective(date);
    }

    public List<Map> getIntrustyIn(Map map) {
            List<Map> resultList = userInfoMapper.getIntrusty();
            for(int i =0;i<resultList.size();i++){
                Set set=map.entrySet();
                Iterator iterator=set.iterator();
                resultList.get(i).put("isIntrested",false);
                while(iterator.hasNext()){
                    Map.Entry<String, Object> enter=(Map.Entry<String, Object>) iterator.next();
                    if(enter.getValue().equals(resultList.get(i).get("industry_name"))){
                        resultList.get(i).put("isIntrested",true);
                    }
                }
            }
            return resultList;
    }
    public List<Map> getIntrusty() {
        return userInfoMapper.getIntrusty();
    }


    public Map getUserIndusty(Integer userid) {
        UserInfo userIfo = userInfoMapper.getUserIndusty(userid);
        Map map = new HashMap();
        String intrest = userIfo.getIntrestIndus();
        if(intrest!=null && !intrest.equals("")){
            String[] intres = intrest.split(",");
            for(int i=0;i<intres.length;i++){
                map.put( intres[i],changeName(Integer.valueOf( intres[i])));
            }
        }
        return map;
    }
    public  String changeName(Integer id){
        if(null==userInfoMapper.getName(id)){
            return "";
        }else{
            return userInfoMapper.getName(id);
        }
    };

    public List<Map> getIntrestIndustyModel(Map map) {
        List<Map> list = userInfoMapper.getIntrestIndustyModel(map);
        Map params = new HashMap();
        params.put("userid",((Integer) map.get("userid")).toString());
        for(int i=0;i<list.size();i++){
                Map model =  ((Map)list.get(i));
                params.put("modelId",(Integer)model.get("id"));
                model.put("orNo",userInfoMapper.getCommentOrNo(params));
                String ids = (String)model.get("model_industry");
                model.put("model_industry",ids);
                /*if(ids != null && !("".equals(ids))){
                    String[] intrestIndusId =  ids.split(",");
                    for(int j = 0; j<intrestIndusId.length;j++){
                        intrestIndus += changeName(Integer.valueOf(intrestIndusId[j]));
                        if(i!=intrestIndusId.length-1){
                            intrestIndus += ",";
                        }
                    }
                    model.put("model_industry",intrestIndus);
                }*/
        }
        return list;
    }

    public List<Map> getCommonModel(Map map) {
        List<Map> list = userInfoMapper.getCommonModel(map);
        for(int i=0;i<list.size();i++){
            String intrestIndus = "";
            String ids = (String)((Map)list.get(i)).get("model_industry");
            if(ids != null && !("".equals(ids))){
                String[] intrestIndusId =  ids.split(",");
                for(int j = 0; j<intrestIndusId.length;j++){
                    intrestIndus += changeName(Integer.valueOf(intrestIndusId[j]));
                    if(i!=intrestIndusId.length-1){
                        intrestIndus += ",";
                    }
                }
                ((Map)list.get(i)).put("model_industry",intrestIndus);
            }
        }
        return list;
    }

    public UserInfo getUser(Integer userid) {
        return userInfoMapper.getUserIndusty(userid);
    }

    public int checkIsTruePwd(Map params) {
        return  userInfoMapper.checkIsTruePwd(params);
    }
}
